package com.demo.demo1;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.os.Build;

public class MainActivity extends ActionBarActivity {
	
	private final IntentFilter intentFilter = new IntentFilter();
	
	private WifiP2pManager manager;
    private Channel channel;
    
	private BroadcastReceiver receiver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
		
		
	}

	public void gotoFileSelectorActivity(View view) {
		Intent intent = new Intent(this, FileSelectorActivity.class);
		startActivity(intent);
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			 
			final WifiManager wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
			boolean wifiEnabled = wifiManager.isWifiEnabled();
			final TextView wifiStatusText = (TextView) rootView.findViewById(R.id.textView1);
			ToggleButton wifiOnOff = (ToggleButton) rootView.findViewById(R.id.toggleButton1);
			wifiOnOff.setChecked(wifiEnabled);
			if(wifiEnabled){
				wifiStatusText.setText("Waiting for incoming connections.");
			}else{
				wifiStatusText.setText("Turn WiFi on");
			}
			
			wifiOnOff.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					wifiManager.setWifiEnabled(isChecked);	
					if(isChecked){
						wifiStatusText.setText("Waiting for incoming connections.");
					}else{
						wifiStatusText.setText("Turn WiFi on");
					}
				}
			});
			
			
			
			return rootView;
		}
	}
	
	
	private void startRecieveMode(){

        intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
        intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
        intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
        intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);

        manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
        channel = manager.initialize(this, getMainLooper(), null);        
        
        receiver = new BroadcastReceiver() {
			
			@Override
			public void onReceive(Context context, Intent intent) {
				 String action = intent.getAction();
			        if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
			        	int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
			            if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {			                
			            	Toast.makeText(MainActivity.this, "Wifi turned on", Toast.LENGTH_SHORT).show();
			            } else {
			            	Toast.makeText(MainActivity.this, "Please turn wifi on", Toast.LENGTH_SHORT).show();
			            }
			        }	        
			        else if(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)){
			        	Toast.makeText(MainActivity.this, "peers changed. reciever", Toast.LENGTH_SHORT).show();
			        }
			        else if(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)){
			        	Toast.makeText(MainActivity.this, "connection changed. reciever", Toast.LENGTH_SHORT).show();
			        } else if(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)){
			        	Toast.makeText(MainActivity.this, "this device changed. reciever", Toast.LENGTH_SHORT).show();
			        } 
			}
		};
        registerReceiver(receiver, intentFilter);
	}

}
